#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <time.h>
#include "tyn_coder.h"

int main() {
/*    
        //int doc_count = 328744;
        int doc_count = 21328744;
        srand ( time(NULL) );
        //int doc_count = 13570000;
        uint32_t *i_ptr;
        uint32_t *in = malloc(doc_count * sizeof(uint32_t));
        uint8_t *encoded = malloc(doc_count * sizeof(uint32_t));
        uint32_t *decoded = malloc(doc_count * sizeof(uint32_t));

        //srand((unsigned int)time(NULL));
        int i;
        size_t bytes_read;

        clock_t t1, t2;
        size_t cnt_encoded;
        size_t cnt_decoded;
        size_t encoded_size;
        double millis;
        int times = 15;
        uint32_t threshold=65536;

        while(times-- > 0 ) {
                threshold >>= 1;
                printf("generating random %d numbers within %d\n", doc_count, threshold);
                for(i = 0; i < doc_count; i++) {
                        *(in + i) = rand() % threshold ;
                        //*(in + i) = 0 ;
                }
                printf("=====================================================\n");
        
        
                t1 = clock();
                encoded_size = iS16_encode32(in, doc_count, encoded);
                t2 = clock();
                millis = (double)(t2 - t1) / CLOCKS_PER_SEC;
                printf("\tiS16 encoding time:\t %f\n",millis);
                printf("\tcompress ratio:\t\t %f\n", (float)encoded_size / (doc_count * 4));
                printf("\tiS16 encoded size:\t %d\n", encoded_size);
                t1 = clock();
                cnt_decoded = iS16_decode32(encoded, decoded, &bytes_read);
                t2 = clock();
                millis = (double)(t2 - t1) / CLOCKS_PER_SEC;
                printf("\tiS16 decoding time:\t %f\n",millis);
                printf("\tdecoded number amount:\t %d\n", cnt_decoded);
                for(i = 0; i < doc_count;i++) {
                        if(*(in+i) != *(decoded+i)) {
                                printf("error\n");
                                return -1;
                        }
                }
        
                printf("\t=============================================\n");
                t1 = clock();
                encoded_size = tyn_p4d_encode32(in, doc_count, encoded, 1);
                t2 = clock();
                millis = (double)(t2 - t1) / CLOCKS_PER_SEC;
                printf("\tp4delta encoding time:\t %f\n",millis);
                printf("\tcompress ratio:\t\t %f\n", (float)encoded_size / (doc_count * 4));
                printf("\tp4d encoded size:\t %d\n", encoded_size);
                t1 = clock();
                cnt_decoded = tyn_p4d_decode32(encoded, decoded, &bytes_read);
                t2 = clock();
                millis = (double)(t2 - t1) / CLOCKS_PER_SEC;
                printf("\tp4d decoding time:\t %f\n",millis);
                printf("\tdecoded number amount:\t %d\n", cnt_decoded);
                
                for(i = 0; i < doc_count;i++) {
                    //printf("%d\n", *(decoded + i));
                        if(*(in+i) != *(decoded+i)) {
                                printf("error\n");
                                return -1;
                        }
                }
                printf("=====================================================\n");
                //exit(1);
        }//while
        free(in);
        free(encoded);
        free(decoded);
        */
// ----------------------------------------------------------------------------------------------------------
    
        
        //int doc_count = 328744;
        srand ( time(NULL) );
        int doc_count = 21328744 + rand()%32767;
        //int doc_count = 13570000;
        uint64_t *i_ptr;
        uint64_t *in = malloc(doc_count * sizeof(uint64_t));
        uint8_t *encoded = malloc(doc_count * sizeof(uint64_t));
        uint64_t *decoded = malloc(doc_count * sizeof(uint64_t));

        //srand((unsigned int)time(NULL));
        int i;
        size_t bytes_read;

        clock_t t1, t2;
        size_t cnt_encoded;
        size_t cnt_decoded;
        size_t encoded_size;
        double millis;
        int times = 32;
        uint64_t threshold= 0xffffffffffffffff;

        while(times-- > 0 ) {
                threshold >>= 2;
                printf("generating random %d numbers within %llu\n", doc_count, threshold);
                for(i = 0; i < doc_count; i++) {
                        *(in + i) = rand() *rand() % threshold ;
                        //*(in + i) = 0 ;
                }
                 printf("=====================================================\n");
        
        
                t1 = clock();
                encoded_size = tyn_iS16_encode64(in, doc_count, encoded);
                t2 = clock();
                millis = (double)(t2 - t1) / CLOCKS_PER_SEC;
                printf("\tiS16 encoding time:\t %f\n",millis);
                printf("\tcompress ratio:\t\t %f\n", (float)encoded_size / (doc_count * 8));
                printf("\tiS16 encoded size:\t %d\n", encoded_size);
                t1 = clock();
                cnt_decoded = tyn_iS16_decode64(encoded, decoded, &bytes_read);
                t2 = clock();
                millis = (double)(t2 - t1) / CLOCKS_PER_SEC;
                printf("\tiS16 decoding time:\t %f\n",millis);
                printf("\tdecoded number amount:\t %d\n", cnt_decoded);
                for(i = 0; i < doc_count;i++) {
                        if(*(in+i) != *(decoded+i)) {
                                printf("error\n");
                                return -1;
                        }
                }       
                //continue;
                printf("\t=============================================\n");
                t1 = clock();
                encoded_size = tyn_p4d_encode64(in, doc_count, encoded, 1);
                t2 = clock();
                millis = (double)(t2 - t1) / CLOCKS_PER_SEC;
                printf("\tp4delta encoding time:\t %f\n",millis);
                printf("\tcompress ratio:\t\t %f\n", (float)encoded_size / (doc_count * 8));
                printf("\tp4d encoded size:\t %d\n", encoded_size);
                t1 = clock();
                cnt_decoded = tyn_p4d_decode64(encoded, decoded, &bytes_read);
                t2 = clock();
                millis = (double)(t2 - t1) / CLOCKS_PER_SEC;
                printf("\tp4d decoding time:\t %f\n",millis);
                printf("\tdecoded number amount:\t %d\n", cnt_decoded);
                
                for(i = 0; i < doc_count;i++) {
                    //printf("%d\n", *(decoded + i));
                        if(*(in+i) != *(decoded+i)) {
                                printf("error\n");
                                return -1;
                        }
                }
                printf("=====================================================\n");
                
        }//while
        free(in);
        free(encoded);
        free(decoded);

}
